package jacker.bit.price;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;

public class ActivitySettings extends PreferenceActivity 
	implements OnPreferenceChangeListener{
	
	//ListPreference notify;
	CheckBoxPreference vibrate;
	ListPreference fetchfrq;
	EditTextPreference uppercent;
	EditTextPreference downpercent;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.config);
		
		addPreferencesFromResource(R.xml.settings);
		
		//notify = (ListPreference)findPreference("mail_notify");
		vibrate = (CheckBoxPreference)findPreference("price_vibrate");
		fetchfrq = (ListPreference)findPreference("fetch_frequency");
		uppercent = (EditTextPreference)findPreference("up_percentage");
		downpercent = (EditTextPreference)findPreference("down_percentage");
		
		uppercent.setSummary(uppercent.getText()+"%");
		downpercent.setSummary(downpercent.getText()+"%");
		setFrqSumm(fetchfrq.getValue());
		
		
		vibrate.setOnPreferenceChangeListener(this);
		uppercent.setOnPreferenceChangeListener(this);
		downpercent.setOnPreferenceChangeListener(this);
		fetchfrq.setOnPreferenceChangeListener(this);
		
		
	}
	
	private void setFrqSumm(String value){
		int i = fetchfrq.findIndexOfValue(value);
		fetchfrq.setSummary( fetchfrq.getEntries()[i] );
	}


	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		
		if(vibrate == preference){
			if(new Boolean(true).equals(newValue))
				PriceChecker.vibrate(this);
			return true;
		}
		
		if(uppercent == preference){
			uppercent.setSummary(newValue+"%");
			//update alart prices
			PriceHelper.notifyUpdate(Consts.MSG_MAIN_DATA_UPDATE,true);
			return true;
		}
		
		if(downpercent == preference){
			downpercent.setSummary(newValue+"%");
			PriceHelper.notifyUpdate(Consts.MSG_MAIN_DATA_UPDATE,true);
			return true;
		}
		
		if(fetchfrq == preference){
			setFrqSumm(newValue.toString());
			return true;
		}
		
		return false;
	}
	

}
